/**
* 基础菜单 商品管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文件分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!--    一个大大框 -->
    <div class="container">
      <h1>文件分类</h1>
    </div>

    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="">
        <el-button style="margin-left: 10px;" size="small" type="success" icon="el-icon-plus" @click="handleEdit()">新增</el-button>
      </el-form-item>

    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <!--      TODO 提供下载pdf、code筛选、hash去重等功能-->
      <el-table-column v-if="false"  align="center" type="selection" width="60">
      </el-table-column>
      <!--      <el-table-column sortable prop="fileName" label="pdf名称" width="300">-->
      <!--      </el-table-column>-->
      <el-table-column sortable prop="tag" align="center" label="文件类别" width="550">
      </el-table-column>
      <!--<el-table-column align="center" sortable prop="createTime" label="创建时间" min-width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="updateTime" label="修改时间" min-width="120">
      </el-table-column>-->
      <!--      <el-table-column sortable prop="fileCode" label="pdf代码" width="200">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column sortable prop="projectCode" label="项目代码" width="200">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column sortable prop="filePath" label="pdf路径" width="300">-->
      <!--      </el-table-column>-->

      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <!--          <el-button size="mini" @click="showPDF(scope.row)">查看pdf内容</el-button>-->
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px"  ref="addtag":rules="rules" :model="addtag">
        <el-form-item label="分类名称" prop="tag">
          <el-input size="small" v-model="addtag.tag" auto-complete="off" placeholder="请输入分类名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm1('addtag')">保存</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
  import { deptList, deptSave, deptDelete } from '../../api/userMG'
  import Pagination from '../../components/Pagination'
  import axios from "axios";
  export default {
    data() {
      return {
        delId : {
          id:""
        },
        addtag: {
          id:'',
          tag:""
        },
        content:"",
        contentShow_:false,
        filename:"test",
        //old_data
        nshow: true, //switch开启
        fshow: false, //switch关闭
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        title: '添加',
        editForm: {
          deptId: '',
          deptName: '',
          deptNo: '',
          token: localStorage.getItem('logintoken')
        },
        // rules表单验证
        rules: {
          tag: [
            { required: true, message: '请输入类别名称', trigger: 'blur' }
          ],
          deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
        },
        formInline: {
          page: 1,
          limit: 100,
          varLable: '',
          varName: '',
          token: localStorage.getItem('logintoken')
        },
        // 删除部门
        seletedata: {
          ids: '',
          token: localStorage.getItem('logintoken')
        },
        userparm: [], //搜索权限
        listData: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    },
    // 注册组件
    components: {
      Pagination
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      this.getdata(this.formInline)
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取公司列表
      getdata(parameter) {

        this.loading = true
        let url = this.$global.javaUrl + "/modelFiletag/list"
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data',
            'user' : localStorage.getItem('userId')
          }
        }
        axios.get(url,{
          headers:
            {
              'user' : localStorage.getItem('userId')
            },
          params:
            {
              pageNum: this.formInline.page,
              pageSize:this.formInline.limit
              // tagId : 0
            }
        }).then(res=>{
          console.log("返回信息")
          console.log(res)
          this.listData = res.data.data.rows


          this.pageparm.total = res.data.data.records
        })

        this.loading = false
        // this.listData = res.data

        // 模拟数据结束


      },
      addTagM(){

        this.editFormVisible = true
        let url = this.$global.javaUrl + "/modelFiletag/saveOrUpdate"
        let config = {
          headers: {
            'user' : localStorage.getItem('userId')
          }
        }

        // let fd = new FormData()// FormData 对象
        // fd.append('tag', this.addtag)// 文件对象
        axios.post(url,this.addtag,{
          headers:
            {
              'user' : localStorage.getItem('userId')
            }
        }).then(res=>{
          console.log("返回信息")
          console.log(res)
          this.$message({
            type: 'success',
            message: '添加成功！'
          })

          this.editFormVisible = false

          this.getdata(this.formInline)
        })
      },
   // 编辑、添加提交方法
      submitForm1(addtag) {
        let config = {
          headers:
            {
              'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            }
        }
        this.$refs[addtag].validate(valid => {
          if (valid)
          {
            // 请求方法
            axios
              .post(this.$global.javaUrl +"/modelFiletag/saveOrUpdate",this.addtag,config)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                this.pageparm.currentPage = this.formInline.page
                this.pageparm.pageSize = this.formInline.limit
                this.pageparm.total = res.data.data.records
                if (res.data.data != null) {
                  this.$message({
                    type: 'success',
                    message: "数据保存成功！"
                  })
                  this.getdata(this.formInline)
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('保存失败，请稍后再试！')
              })
          } /*else {
            this.$message({
              type: 'warning',
              message: '未输入相关信息，无法保存！'
            })
          }*/
        })
      },

      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getdata(this.formInline)
      },
      // 搜索事件
      search() {
        this.getdata(this.formInline)
      },
      //显示编辑界面
      handleEdit: function(index, row) {
        this.editFormVisible = true

          this.title = '添加'
          this.addtag.id = ''
          this.addtag.tag = ''

      },
      // 编辑、增加页面保存方法
      submitForm(editData) {
        this.editFormVisible = true
        // this.addTagM()
      },

      showPDF( row) {
        this.content = row.content
        this.contentShow_ = true
        console.log(row)
      },
      // 删除公司
      deleteUser(index, row) {
        var dId = row.id
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.delId.id = dId
            let url = this.$global.javaUrl + "/modelFiletag/delete"
            let config = {
              headers: {
                'Content-Type': 'multipart/form-data',
                'user' : localStorage.getItem('userId')
              }
            }
            axios.post(url,this.delId,{
              headers:
                {
                  'user' : localStorage.getItem('userId')
                }
            }).then(res => {
              if (res.status == 200) {
                console.log("res",res)
                if(res.data.data == 0){
                  this.$message({
                    type: 'success',
                    message: '该分类包含关联文件!'
                  })
                }else{
                  this.$message({
                    type: 'success',
                    message: '数据已删除!'
                  })
                  this.getdata(this.formInline)
                }

              } else {

              }
            })
              .catch(err => {
                this.loading = false
                this.$message.error('数据删除失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false
        this.contentshow_ = false
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }
  .userRole {
    width: 100%;
  }
  .container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 10vh; /* 或者设置一个固定高度 */
  }
</style>


